package net.vanchu.mortgagecalculator.mortgage.resultShow;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dtspread.apps.houseloan.R;

import net.vanchu.mortgagecalculator.mortgage.data.monthRepayment.MixMonthRepay;
import net.vanchu.mortgagecalculator.mortgage.data.repaymentSummary.PrincipalBalancedPart;
import net.vanchu.mortgagecalculator.mortgage.data.repaymentSummary.RepaySummary;
import net.vanchu.mortgagecalculator.mortgage.data.repaymentSummary.TotalBalancedPart;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Granton on 2016/12/26.
 */
class MixWayResultDataAdapter extends RecyclerView.Adapter<MixWayResultDataAdapter.MixWayResultDataViewHolder> {

    private ComplainButtonListener complainButtonListener;

    private static final String TAG = MixWayResultDataAdapter.class.getSimpleName();

    private List<ResultData<RepaySummary.Mix>> dataList = new ArrayList<>();

    MixWayResultDataAdapter(List<ResultData<RepaySummary.Mix>> dataList,
                            ComplainButtonListener complainButtonListener) {
        this.dataList = dataList;
        this.complainButtonListener = complainButtonListener;
    }

    @Override
    public MixWayResultDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return dispatchDataToView(viewType, parent);
    }

    @Override
    public void onBindViewHolder(MixWayResultDataViewHolder holder, int position) {
        holder.bind(dataList.get(position));
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getResultType();
    }

    abstract class MixWayResultDataViewHolder extends RecyclerView.ViewHolder {

        MixWayResultDataViewHolder(ViewGroup parent, int resId) {
            super(LayoutInflater.from(parent.getContext()).inflate(resId, parent, false));
        }

        public abstract void bind(ResultData<RepaySummary.Mix> resultData);
    }

    private class ResultShowTableFooter extends MixWayResultDataViewHolder {

        ResultShowTableFooter(ViewGroup parent) {
            super(parent, R.layout.item_result_show_table_footer);
        }

        @Override
        public void bind(ResultData<RepaySummary.Mix> resultData) {

        }
    }

    private MixWayResultDataViewHolder dispatchDataToView(int viewType, ViewGroup parent) {
        if (ResultData.ITEM_TYPE_MIX_TOTAL_BALANCED_SUM == viewType) {
            return new MixTotalBalancedSumViewHolder(parent);
        }
        if (ResultData.ITEM_TYPE_MIX_PRINCIPAL_BALANCED_SUM == viewType) {
            return new MixPrincipalBalancedSumViewHolder(parent);
        }
        if (ResultData.ITEM_TYPE_RESULT_SHOW_COMPLAIN == viewType) {
            return new ComplainEntranceViewHolder(parent);
        }
        if (ResultData.ITEM_TYPE_MIX_TABLE_HEADER == viewType) {
            return new MixWayRepayTableHeader(parent);
        }
        if (ResultData.ITEM_TYPE_MIX_TOTAL_BALANCED_TABLE_DATA_ODD_INDEX == viewType
                || ResultData.ITEM_TYPE_MIX_PRINCIPAL_BALANCED_TABLE_DATA_ODD_INDEX == viewType) {
            return new MixWayRepayTableDataOddIndex(parent);
        }
        if (ResultData.ITEM_TYPE_MIX_TOTAL_BALANCED_TABLE_DATA_EVEN_INDEX == viewType
                || ResultData.ITEM_TYPE_MIX_PRINCIPAL_BALANCED_TABLE_DATA_EVEN_INDEX == viewType) {
            return new MixWayRepayTableDataEvenIndex(parent);
        }
        if (ResultData.ITEM_TYPE_RESULT_SHOW_FOOTER == viewType) {
            return new ResultShowTableFooter(parent);
        }
        return null;
    }

    private class MixWayRepayTableDataEvenIndex extends MixWayResultDataViewHolder {

        private final TextView tableDataIndexOfMonth;
        private final TextView tableDataTotalToRepay;
        private final TextView tableDataComRepay;
        private final TextView tableDataProRepay;

        private final List<MixMonthRepay> mixMonthRepays = new ArrayList<>();

        MixWayRepayTableDataEvenIndex(ViewGroup parent) {
            super(parent, R.layout.item_mix_way_repay_table_data_even_index);

            tableDataIndexOfMonth = (TextView) itemView.findViewById(R.id.table_data_index_of_month);
            tableDataTotalToRepay = (TextView) itemView.findViewById(R.id.table_data_total_repay);
            tableDataComRepay = (TextView) itemView.findViewById(R.id.table_data_com_repay);
            tableDataProRepay = (TextView) itemView.findViewById(R.id.table_data_pro_repay);
        }

        @Override
        public void bind(ResultData<RepaySummary.Mix> resultData) {
            if (mixMonthRepays.size() == 0) {
                TotalBalancedPart.Mix totalBalancedPart = resultData.getRepaySummary().getTotalBalancedMix();
                PrincipalBalancedPart.Mix principalBalancedPart = resultData.getRepaySummary().getPrincipalBalancedMix();
                if (resultData.getResultType() == ResultData.ITEM_TYPE_MIX_TOTAL_BALANCED_TABLE_DATA_EVEN_INDEX) {
                    mixMonthRepays.addAll(totalBalancedPart.getMixMonthRepays());
                } else {
                    mixMonthRepays.addAll(principalBalancedPart.getMixMonthRepays());
                }
            }
            MixMonthRepay mixMonthRepay = mixMonthRepays.get(getAdapterPosition() - 3);
            tableDataIndexOfMonth.setText(String.valueOf(mixMonthRepay.getIndex()));
            tableDataTotalToRepay.setText(FormatHelper.formatDouble(mixMonthRepay.getTotalRepayNumber()));
            tableDataComRepay.setText(FormatHelper.formatDouble(mixMonthRepay.getCommercialPart().getTotalRepay()));
            tableDataProRepay.setText(FormatHelper.formatDouble(mixMonthRepay.getProvidentFundPart().getTotalRepay()));
        }
    }

    private class MixWayRepayTableDataOddIndex extends MixWayResultDataViewHolder {

        private final TextView tableDataIndexOfMonth;
        private final TextView tableDataTotalToRepay;
        private final TextView tableDataComRepay;
        private final TextView tableDataProRepay;

        MixWayRepayTableDataOddIndex(ViewGroup parent) {
            super(parent, R.layout.item_mix_way_repay_table_data_odd_index);

            tableDataIndexOfMonth = (TextView) itemView.findViewById(R.id.table_data_index_of_month);
            tableDataTotalToRepay = (TextView) itemView.findViewById(R.id.table_data_total_repay);
            tableDataComRepay = (TextView) itemView.findViewById(R.id.table_data_com_repay);
            tableDataProRepay = (TextView) itemView.findViewById(R.id.table_data_pro_repay);
        }

        @Override
        public void bind(ResultData<RepaySummary.Mix> resultData) {
            TotalBalancedPart.Mix totalBalancedPart = resultData.getRepaySummary().getTotalBalancedMix();
            PrincipalBalancedPart.Mix principalBalancedPart = resultData.getRepaySummary().getPrincipalBalancedMix();
            List<MixMonthRepay> mixMonthRepays =
                    resultData.getResultType() == ResultData.ITEM_TYPE_MIX_TOTAL_BALANCED_TABLE_DATA_ODD_INDEX ?
                            totalBalancedPart.getMixMonthRepays() :
                            principalBalancedPart.getMixMonthRepays();
            MixMonthRepay mixMonthRepay = mixMonthRepays.get(getAdapterPosition() - 3);
            tableDataIndexOfMonth.setText(String.valueOf(mixMonthRepay.getIndex()));
            tableDataTotalToRepay.setText(FormatHelper.formatDouble(mixMonthRepay.getTotalRepayNumber()));
            tableDataComRepay.setText(FormatHelper.formatDouble(mixMonthRepay.getCommercialPart().getTotalRepay()));
            tableDataProRepay.setText(FormatHelper.formatDouble(mixMonthRepay.getProvidentFundPart().getTotalRepay()));
        }
    }

    private class MixWayRepayTableHeader extends MixWayResultDataViewHolder {

        MixWayRepayTableHeader(ViewGroup parent) {
            super(parent, R.layout.item_mix_way_repay_table_header);
        }

        @Override
        public void bind(ResultData<RepaySummary.Mix> resultData) {

        }
    }

    private class ComplainEntranceViewHolder extends MixWayResultDataViewHolder {

        private final TextView resultShowBtnComplainEntrance;

        ComplainEntranceViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_complain_entrance);
            resultShowBtnComplainEntrance = (TextView) itemView.findViewById(R.id.result_show_btn_complain_entrance);
        }

        @Override
        public void bind(final ResultData<RepaySummary.Mix> resultData) {
            resultShowBtnComplainEntrance.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    complainButtonListener.onClickComplainButton();
                }
            });
        }
    }

    private class MixPrincipalBalancedSumViewHolder extends MixWayResultDataViewHolder {

        private final TextView resultSumTotalPayTotal;
        private final TextView resultSumFirstMonthNumber;
        private final TextView resultSumDecreaseNumber;
        private final TextView resultSumInterestTotalPay;
        private final TextView resultSumComRepayTotal;
        private final TextView resultSumProRepayTotal;

        MixPrincipalBalancedSumViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_mix_reapy_sum_principal_balanced);
            resultSumTotalPayTotal = (TextView) itemView.findViewById(R.id.result_sum_total_pay_total);
            resultSumFirstMonthNumber = (TextView) itemView.findViewById(R.id.result_sum_first_month_number);
            resultSumDecreaseNumber = (TextView) itemView.findViewById(R.id.result_sum_decrease_number);
            resultSumInterestTotalPay = (TextView) itemView.findViewById(R.id.result_sum_interest_total_pay);
            resultSumComRepayTotal = (TextView) itemView.findViewById(R.id.result_sum_com_repay_total);
            resultSumProRepayTotal = (TextView) itemView.findViewById(R.id.result_sum_pro_repay_total);
        }

        @Override
        public void bind(ResultData<RepaySummary.Mix> resultData) {
            PrincipalBalancedPart.Mix principalBalancedPart = resultData.getRepaySummary().getPrincipalBalancedMix();
            double totalRepayCom = principalBalancedPart.getComPrincipalAndInterest().getTotalRepay();
            double totalRepayPro = principalBalancedPart.getProPrincipalAndInterest().getTotalRepay();
            double totalRepayNumber = totalRepayCom + totalRepayPro;
            double firstRepayNumber = principalBalancedPart.getFirstRepayNumber();
            double decreaseNumber = principalBalancedPart.getDecreaseNumber();
            double totalInterest = principalBalancedPart.getComPrincipalAndInterest().getInterest() +
                    principalBalancedPart.getProPrincipalAndInterest().getInterest();
            resultSumTotalPayTotal.setText(FormatHelper.formatDouble(totalRepayNumber));
            resultSumFirstMonthNumber.setText(FormatHelper.formatDouble(firstRepayNumber));
            resultSumDecreaseNumber.setText(FormatHelper.formatDouble(decreaseNumber));
            resultSumInterestTotalPay.setText(FormatHelper.formatDouble(totalInterest));
            resultSumComRepayTotal.setText(FormatHelper.formatDouble(totalRepayCom));
            resultSumProRepayTotal.setText(FormatHelper.formatDouble(totalRepayPro));
        }
    }

    private class MixTotalBalancedSumViewHolder extends MixWayResultDataViewHolder {

        private final TextView resultSumTotalPayTotal;
        private final TextView resultSumPerMonthPay;
        private final TextView resultSumInterestTotalPay;
        private final TextView resultSumComRepayTotal;
        private final TextView resultSumProRepayTotal;

        MixTotalBalancedSumViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_mix_reapy_sum_total_balanced);

            resultSumTotalPayTotal = (TextView) itemView.findViewById(R.id.result_sum_total_pay_total);
            resultSumPerMonthPay = (TextView) itemView.findViewById(R.id.result_sum_per_month_pay);
            resultSumInterestTotalPay = (TextView) itemView.findViewById(R.id.result_sum_interest_total_pay);
            resultSumComRepayTotal = (TextView) itemView.findViewById(R.id.result_sum_com_repay_total);
            resultSumProRepayTotal = (TextView) itemView.findViewById(R.id.result_sum_pro_repay_total);
        }

        @Override
        public void bind(ResultData<RepaySummary.Mix> resultData) {
            TotalBalancedPart.Mix totalBalancedMix = resultData.getRepaySummary().getTotalBalancedMix();
            double totalRepayCom = totalBalancedMix.getComPrincipalAndInterest().getTotalRepay();
            double totalRepayPro = totalBalancedMix.getProPrincipalAndInterest().getTotalRepay();
            double totalRepayNumber = totalRepayCom + totalRepayPro;
            double numberRepayPerMonth = totalBalancedMix.getNumberRepayPerMonth();
            double totalInterest = totalBalancedMix.getComPrincipalAndInterest().getInterest() +
                    totalBalancedMix.getProPrincipalAndInterest().getInterest();
            resultSumTotalPayTotal.setText(FormatHelper.formatDouble(totalRepayNumber));
            resultSumComRepayTotal.setText(FormatHelper.formatDouble(totalRepayCom));
            resultSumProRepayTotal.setText(FormatHelper.formatDouble(totalRepayPro));
            resultSumPerMonthPay.setText(FormatHelper.formatDouble(numberRepayPerMonth));
            resultSumInterestTotalPay.setText(FormatHelper.formatDouble(totalInterest));
        }
    }
}